package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.CWaybillDetailService;
import cn.huiyunche.base.service.mappers.CWaybillDetailMapper;
import cn.huiyunche.base.service.model.CWaybillDetail;
import cn.huiyunche.base.service.model.CWaybillDetailExample;
import cn.huiyunche.base.service.model.CWaybillDetailExample.Criteria;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CWaybillDetailServiceImpl implements CWaybillDetailService {

    private static final Logger logger = LoggerFactory.getLogger(CWaybillDetailServiceImpl.class);

    @Autowired
    private CWaybillDetailMapper cWaybillDetailMapper;

    @Override
    public List<CWaybillDetail> getList(long[] serviceOrderIds, int dispatchTimes, Boolean isLastTime) {
        logger.info("getList params : {}, {}, {}", serviceOrderIds.length, dispatchTimes, isLastTime);
        if (null == serviceOrderIds || 0 == serviceOrderIds.length) {
            logger.error("getList serviceOrderIds must not be null|length is 0");
            throw new IllegalArgumentException("服务订单id不能为空！");
        }
//		if(0==dispatchTimes){
//			logger.error("dispatchTimes must not be 0");
//			throw new IllegalArgumentException("调度次数不能是0！");
//		}

        List<Long> ids = new ArrayList<>();
        for (Long i : serviceOrderIds) {
            ids.add(i);
        }
        CWaybillDetailExample example = new CWaybillDetailExample();
        Criteria criteria = example.createCriteria().andServiceOrderIdIn(ids);
        if (0 != dispatchTimes) {
            criteria.andServiceDispatchTimesEqualTo(dispatchTimes);
        }
        if (null != isLastTime) {
            criteria.andIsLastTimeEqualTo(isLastTime);
        }
        List<CWaybillDetail> list = cWaybillDetailMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

    @Override
    public CWaybillDetail getByIdAndDispatchTimes(long serviceOrderId, int dispatchTimes) {
        logger.info("getByIdAndDispatchTimes params : {}, {}", serviceOrderId, dispatchTimes);
        if (0 == serviceOrderId) {
            logger.error("getByIdAndDispatchTimes serviceOrderId must not be 0");
            throw new IllegalArgumentException("服务订单id不能为空！");
        }
        if (0 == dispatchTimes) {
            logger.error("getByIdAndDispatchTimes dispatchTimes must not be 0");
            throw new IllegalArgumentException("调度次数不能是0！");
        }

        CWaybillDetailExample example = new CWaybillDetailExample();
        example.createCriteria().andServiceOrderIdEqualTo(serviceOrderId).andServiceDispatchTimesEqualTo(dispatchTimes);
        List<CWaybillDetail> list = cWaybillDetailMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            logger.info("getByIdAndDispatchTimes list is empty");
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<CWaybillDetail> getListByWaybillId(long waybillId) {
        logger.info("getListByWaybillId param : {}", waybillId);
        if (0 == waybillId) {
            logger.error("getListByWaybillId waybill must not be 0");
            throw new IllegalArgumentException("运单id不能是0");
        }

        CWaybillDetailExample example = new CWaybillDetailExample();
        example.createCriteria().andWaybillIdEqualTo(waybillId);
        List<CWaybillDetail> list = cWaybillDetailMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            logger.error("getListByWaybillId list is empty");
            throw new BusinessException("获取运单车辆信息失败！");
        }
        return list;
    }

}
